﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LibrarySystem
{
    public partial class WebForm10 : System.Web.UI.Page
    {
        private int _aid;

        protected void Page_Load(object sender, EventArgs e)
        {
            _aid = Convert.ToInt32(Request.QueryString["aid"]);
            BusinessLayer.Author author = BusinessLayer.Author.getByAid(_aid);
            List<BusinessLayer.Book> books = BusinessLayer.Book.getByAuthor(_aid);

            if (author != null)
            {
                authorDetailName.Text = author.FirstName + " " + author.LastName;
                authorDetailYear.Text = (author.BirthYear == 0) ? "" : "(" + author.BirthYear.ToString() + ")";
            }

            if (books.Count != 0)
            {
                authorDetailCount.Text = "Antal böcker: " + books.Count;
                bookDetailCopyList.DataSource = books;
                bookDetailCopyList.DataBind();
            }

        }
        protected void listItem_ClickedBook(object sender, CommandEventArgs e)
        {
            string itemISBN = e.CommandName.ToString();
            Response.Redirect("adminBookDetail.aspx?isbn=" + itemISBN);
        }


        protected void adminCtrlModify_Click(object sender, EventArgs e)
        {
            if (adminModify.Visible == false)
            {
                BusinessLayer.Author thisAuthor = BusinessLayer.Author.getByAid(_aid);
                modifyFirstName.Text = thisAuthor.FirstName;
                modifyLastName.Text = thisAuthor.LastName;
                modifyBirth.Text = thisAuthor.BirthYear.ToString();
                adminModify.Visible = true;
            }
            else
                adminModify.Visible = false;
        }

        protected void adminCtrlRemove_Click(object sender, EventArgs e)
        {
            BusinessLayer.Author.DeleteAuthor(_aid);
            Response.Redirect("adminSearch.aspx?val=bkTitle&term=");
        }

        protected void adminCtrlSave_Click(object sender, EventArgs e)
        {
            BusinessLayer.Author.ModAuthor(_aid, modifyFirstName.Text, modifyLastName.Text, Convert.ToInt32(modifyBirth.Text));
            Response.Redirect(Request.RawUrl);
        }
    }
}